package com.example.controller;


import com.example.common.Result;
import com.example.entity.User;
import com.example.service.AdminService;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService  userService;
    @Autowired
    AdminService  adminService;


    @PutMapping("/update")
    public Result update (@RequestBody User user)
    {
        if (user.getRole().equals("普通用户"))
            userService.updateById(user);
        else if (user.getRole().equals("管理员")) {
            adminService.updateById(user);
        }

        return Result.success();
    }



    /**
     * 新增
     */

    @PostMapping("/add")
    public Result add (@RequestBody User user){
        userService.add(user);
        return Result.success();
    }

    /**
     * 删除
     */

    @DeleteMapping("/delete/{id}")
    public Result delete (@PathVariable Integer id){
        userService.deleteById(id);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize,
                             User user){
        PageInfo<User> PageInfo = userService.selectPage(pageNum,pageSize,user);
        return Result.success(PageInfo);
    }




}
